package com.wzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.common.pojo.resp.Result;
import com.wzx.user.dto.NotesDto;
import com.wzx.user.dto.RedPkgDto;
import com.wzx.user.mapper.NotesMapper;
import com.wzx.user.pojo.Notes;
import com.wzx.user.service.NotesService;
import com.wzx.user.utils.AmountUtils;
import com.wzx.user.utils.RedPacketUtils;
import com.wzx.user.utils.RedisUtils;
import com.wzx.user.vo.NotesVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wzx
 * @version 1.0
 * @date 2023/3/1 - 22:52
 * @description 标题
 */
@Service
public class NotesServiceImpl extends ServiceImpl<NotesMapper, Notes> implements NotesService {

    @Autowired
    private NotesMapper notesMapper;

    @Autowired
    private RedPacketUtils redPacketUtils;

    @Autowired
    private RedisUtils redisUtil;

    @Override
    public List<Notes> queryNotesList(String method, Integer userId) {
        QueryWrapper<Notes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        if (!StringUtils.isEmpty(method)) {
            queryWrapper.eq("method", method);
        }
        return notesMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> queryPayAndIncome(NotesVo notesVo) {
        Integer sumPrice = notesMapper.selectSumPrice(notesVo.getUserId(), "add", notesVo.getYear(), notesVo.getMouth());
        Integer reducePrice = notesMapper.selectSumPrice(notesVo.getUserId(), "reduce", notesVo.getYear(), notesVo.getMouth());

        Page<NotesDto> page = new Page<>();
        page.setCurrent(notesVo.getPageNum());
        page.setSize(notesVo.getPageSize());

        IPage<NotesDto> oddsIPage = notesMapper.queryById(page,
                notesVo.getUserId(),
                notesVo.getYear(),
                notesVo.getMouth());

        //数据处理
        List<NotesDto> resultList = oddsIPage.getRecords();
        for (NotesDto notesObject : resultList) {
            if (notesObject.getMethod().equals("5") && notesObject.getRedPkgId() != null) {
                //查询redis
                //红包信息缓存
                String redPackageKey = RedPacketUtils.RED_PACKAGE_KEY + notesObject.getRedPkgId();
                //获取红包相关信息
                RedPkgDto redPkgDto = (RedPkgDto) redisUtil.get(redPackageKey);
                if (redPkgDto != null && redPkgDto.getSurplusNum() != null) {
                    notesObject.setSurplusNum(redPkgDto.getSurplusNum());
                    notesObject.setTotalNum(redPkgDto.getTotalNum());
                }
            }
        }

        /*return Result.successByPage(oddsIPage.getCurrent(),
                oddsIPage.getSize(),
                oddsIPage.getTotal(),
                oddsIPage.getPages(),
                oddsIPage.getRecords());*/

        //List<NotesDto> list = notesMapper.queryById(notesVo.getUserId(), notesVo.getYear(), notesVo.getMouth());

        Map<String, Object> map = new HashMap<>();
        map.put("sumPrice", AmountUtils.handlerAmountByDouble(sumPrice));
        map.put("reducePrice", AmountUtils.handlerAmountByDouble(reducePrice));
        map.put("list", resultList);
        return map;
    }

    @Override
    public Result queryPayAndIncomeByPage(NotesVo notesVo) {
        Page<NotesDto> page = new Page<>();
        page.setCurrent(notesVo.getPageNum());
        page.setSize(notesVo.getPageSize());

        IPage<NotesDto> oddsIPage = notesMapper.queryById(page,
                notesVo.getUserId(),
                notesVo.getYear(),
                notesVo.getMouth());

        //数据处理
        List<NotesDto> resultList = oddsIPage.getRecords();
        for (NotesDto notesObject : resultList) {
            if (notesObject.getMethod().equals("5") && notesObject.getRedPkgId() != null) {
                //查询redis
                //红包信息缓存
                String redPackageKey = RedPacketUtils.RED_PACKAGE_KEY + notesObject.getRedPkgId();
                //获取红包相关信息
                RedPkgDto redPkgDto = (RedPkgDto) redisUtil.get(redPackageKey);
                if (redPkgDto != null && redPkgDto.getSurplusNum() != null) {
                    notesObject.setSurplusNum(redPkgDto.getSurplusNum());
                    notesObject.setTotalNum(redPkgDto.getTotalNum());
                }
            }
        }

        return Result.successByPage(oddsIPage.getCurrent(),
                oddsIPage.getSize(),
                oddsIPage.getTotal(),
                oddsIPage.getPages(),
                resultList);
    }
}
